package com.slz3.business.module.configManagement.conActivityIndex.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conActivityIndex.domain.ConActivityIndex;
import com.slz3.business.module.configManagement.conActivityIndex.domain.ConActivityIndexFactory;
import com.slz3.business.module.configManagement.conActivityIndex.steward.ConActivityIndexSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConActivityIndexServiceImpl implements ConActivityIndexService {

    @Override
    public ConActivityIndex save(ConActivityIndex conActivityIndex) {
        return Repositorys.conActivityIndex.save(ConActivityIndexFactory.initBaseFields(conActivityIndex));
    }

    @Override
    public Page<ConActivityIndex> page(MultiValueMap<String, String> params) {
        Page<ConActivityIndex> conActivityIndexPage = Repositorys.conActivityIndex.findAll(
                ConActivityIndexSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conActivityIndexPage;
    }

    @Override
    public ConActivityIndex modify(ConActivityIndex conActivityIndexNew) {
        return Repositorys.conActivityIndex.saveAndFlush(ConActivityIndexFactory.initBaseFields(conActivityIndexNew));
    }

    @Override
    public Optional<ConActivityIndex> single(UUID uuid) {
        return Repositorys.conActivityIndex.findById(uuid);
    }

    @Override
    public List<ConActivityIndex> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conActivityIndex.findAllById(uuids);
    }

    @Override
    public List<ConActivityIndex> delete(List<ConActivityIndex> conActivityIndexList) {
        conActivityIndexList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConActivityIndexFactory.initBaseFields(item);
        });
        return Services.conActivityIndexTransactional.batchModify(conActivityIndexList);
    }

}
